package com.mooc.mymooc.service;

import com.mooc.mymooc.dao.*;
import com.mooc.mymooc.pojo.*;
import com.mooc.mymooc.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "moocService")
public class MoocServiceImpl implements MoocService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private EmailDao emailDao;
    @Autowired
    private OrgDao orgDao;
    @Autowired
    private CityDao cityDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private UserAskDao userAskDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private UserFavoriteDao userFavoriteDao;
    @Autowired
    private UserCourseDao userCourseDao;
    @Autowired
    private LessonDao lessonDao;
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private CommentsDao commentsDao;
    @Autowired
    private AdminDao adminDao;
    @Override
    public User login(String username, String password) {
        return this.userDao.login(username,password);
    }

    @Override
    public void register(User user) {
        this.userDao.register(user);
    }

    @Override
    public Integer checkActive(Integer id) {
        return this.userDao.checkActive(id);
    }

    @Override
    public User findById(Integer id) {
        return this.userDao.findById(id);
    }

    @Override
    public Integer updateUser(User user) {
        return this.userDao.updateUser(user);
    }

    @Override
    public Integer updateImage(User user) {
        return this.userDao.updateImage(user);
    }

    @Override
    public Integer insertEmailCode(Email email) {
        return this.emailDao.insertEmailCode(email);
    }

    @Override
    public String findCode(String email) {
        return this.emailDao.findCode(email);
    }

    @Override
    public Integer updateEmail(User user) {
        return this.userDao.updateEmail(user);
    }

    @Override
    public Integer findByEmail(String email) {
        return this.emailDao.findByEmail(email);
    }

    @Override
    public void removeByEmail(String email) {
        this.emailDao.removeByEmail(email);
    }

    @Override
    public Integer updatePwd(User user) {
        return this.userDao.updatePwd(user);
    }

    @Override
    public List<Organization> findAllOrg(Organization organization , String sort, PageModel pageModel) {
        Map map = new HashMap();
        map.put("orgi",organization);
        if (sort!=null) {
            if (sort.equals("students")) {
                map.put("sort", "click_nums");
            }
        }
        Integer counts = this.orgDao.count();
        List<Organization> list = findAllButNoIdea(map);
        pageModel.setRecordCount(list.size());
        if (counts>0){
            map.put("pageModel",pageModel);
        }
        List<Organization> organizationList =  this.orgDao.findAllOrg(map);
        return organizationList;
    }

    @Override
    public City findById(String id) {
        return this.cityDao.findById(id);
    }

    @Override
    public List<Course> findCourseById(Integer id) {
        return this.courseDao.findCourseById(id);
    }

    @Override
    public List<City> findAllCity() {
        return this.cityDao.findAllCity();
    }

    @Override
    public Integer count() {
        return this.orgDao.count();
    }

    @Override
    public List<Organization> findTop4Org() {
        return this.orgDao.findTop4Org();
    }

    @Override
    public Integer insertUserAsk(UserAsk userAsk) {
        return this.userAskDao.insertUserAsk(userAsk);
    }

    @Override
    public List<Organization> findAllButNoIdea(Map map) {
        return this.orgDao.findAllButNoIdea(map);
    }

    @Override
    public Organization findOrgById(Integer id) {
        return this.orgDao.findOrgById(id);
    }

    @Override
    public List<Teacher> findTeacherByOrg(Integer id) {
        return this.teacherDao.findTeacherByOrg(id);
    }

    @Override
    public List<Course> findCourseByIdd(Integer id) {
        return this.courseDao.findCourseByIdd(id);
    }

    @Override
    public Integer insertUserFav(UserFavorite userFavorite) {
        return this.userFavoriteDao.insertUserFav(userFavorite);
    }

    @Override
    public Integer findUserFav(UserFavorite userFavorite) {
        return this.userFavoriteDao.findUserFav(userFavorite);
    }

    @Override
    public Integer removeUserFav(Integer id) {
        return this.userFavoriteDao.removeUserFav(id);
    }

    @Override
    public UserFavorite findIsFavIndex(UserFavorite userFavorite) {
        return this.userFavoriteDao.findIsFavIndex(userFavorite);
    }

    @Override
    public Integer addFav_nums(Integer id) {
        return this.courseDao.addFav_nums(id);
    }

    @Override
    public Integer removeFav_nums(Integer id) {
        return this.courseDao.removeFav_nums(id);
    }

    @Override
    public Integer addFav_nums_org(Integer id) {
        return this.orgDao.addFav_nums_org(id);
    }

    @Override
    public Integer removeFav_nums_org(Integer id) {
        return this.orgDao.removeFav_nums_org(id);
    }

    @Override
    public List<Course> findByPage(Map map) {
        return this.courseDao.findByPage(map);
    }

    @Override
    public List<Course> findAllCourse(PageModel pageModel,String sort) {
        int countsRecord = this.courseDao.countsCourse();
        if (countsRecord>0){
            pageModel.setRecordCount(countsRecord);
        }
        Map map = new HashMap();
        map.put("pageModel",pageModel);
        map.put("sort",sort);
        return this.courseDao.findAllCourse(map);
    }

    @Override
    public Integer countsCourse() {
        return this.courseDao.countsCourse();
    }

    @Override
    public Teacher findTeacherById(Integer id) {
        return this.teacherDao.findTeacherById(id);
    }

    @Override
    public Course findCourseByIdDetail(Integer id) {
        return this.courseDao.findCourseByIdDetail(id);
    }

    @Override
    public Integer insertStartStudy(TempCourse userCourse) {
        return this.userCourseDao.insertStartStudy(userCourse);
    }

    @Override
    public List<Lesson> findLessonById(Integer id) {
        return this.lessonDao.findLessonById(id);
    }

    @Override
    public List<CourseVideo> findVideoById(Integer id) {
        return this.videoDao.findVideoById(id);
    }

    @Override
    public Integer findUserCourseExits(TempCourse userCourse) {
        return this.userCourseDao.findUserCourseExits(userCourse);
    }

    @Override
    public Integer updateStudents(Integer id) {
        return this.courseDao.updateStudents(id);
    }

    @Override
    public List<CourseComments> findAllCommentsByOrgId(Integer id) {
        return this.commentsDao.findAllCommentsByOrgId(id);
    }

    @Override
    public Integer addCourseComments(CourseComments courseComments) {
        return this.commentsDao.addCourseComments(courseComments);
    }

    @Override
    public Admin findAdmin(Admin admin) {
        return this.adminDao.findAdmin(admin);
    }

    @Override
    public Integer addCourseOrg(Organization organization) {
        return this.orgDao.addCourseOrg(organization);
    }

    @Override
    public List<Organization> findAllOrgUseInBackGround(Organization organization) {
        return this.orgDao.findAllOrgUseInBackGround(organization);
    }

    @Override
    public Integer updateOrg(Organization organization) {
        return this.orgDao.updateOrg(organization);
    }

    @Override
    public Integer removeOrg(Integer id) {
        return this.orgDao.removeOrg(id);
    }

    @Override
    public Integer addCourse(Course course) {
        return this.courseDao.addCourse(course);
    }

    @Override
    public List<Course> findAllCourseInBackGround(Course course) {
        return this.courseDao.findAllCourseInBackGround(course);
    }

    @Override
    public Integer removeCourse(Integer id) {
        return this.courseDao.removeCourse(id);
    }

    @Override
    public Integer updateCourse(Course course) {
        return this.courseDao.updateCourse(course);
    }

    @Override
    public Integer addLesson(Lesson lesson) {
        return this.lessonDao.addLesson(lesson);
    }

    @Override
    public Integer updateLesson(Lesson lesson) {
        return this.lessonDao.updateLesson(lesson);
    }

    @Override
    public Integer addVideo(CourseVideo courseVideo) {
        return this.videoDao.addVideo(courseVideo);
    }

    @Override
    public Lesson findLessonByIdInBackGround(Integer id) {
        return this.lessonDao.findLessonByIdInBackGround(id);
    }

    @Override
    public List<CourseComments> findAllComment(CourseComments courseComments) {
        return this.commentsDao.findAllComment(courseComments);
    }

    @Override
    public List<User> findUserByName(String name) {
        return this.userDao.findUserByName(name);
    }

    @Override
    public Integer deleteComments(Integer id) {

        return this.commentsDao.deleteComments(id);
    }

    @Override
    public List<Teacher> findAllTeacher(PageModel pageModel,String sort) {
        Map map = new HashMap();
        map.put("pageModel",pageModel);
        map.put("sort",sort);
        return this.teacherDao.findAllTeacher(map);
    }

    @Override
    public Integer countTeachers() {
        return this.teacherDao.countTeachers();
    }

    @Override
    public Integer updateFavnumsreduce(Integer id) {
        return this.teacherDao.updateFavnumsreduce(id);
    }

    @Override
    public Integer updateFavnumsadd(Integer id) {
        return this.teacherDao.updateFavnumsadd(id);
    }

    @Override
    public Integer addUser(User user) {
        return this.userDao.addUser(user);
    }

    @Override
    public User findUserByEmail(String email) {
        return this.userDao.findUserByEmail(email);
    }

    @Override
    public List<UserMessage> findMessageByUser(Integer id) {
        return userDao.findMessageByUser(id);
    }

    @Override
    public Integer setMessageRead(Integer id) {
        return this.userDao.setMessageRead(id);
    }

    @Override
    public Integer sendMessageToUser(UserMessage userMessage) {
        return this.userDao.sendMessageToUser(userMessage);
    }

    @Override
    public List<User> findAllUser() {
        return this.userDao.findAllUser();
    }

    @Override
    public List<UserCourse> findUserCourseInUserCenter(User user) {
        return this.userCourseDao.findUserCourseInUserCenter(user);
    }

    @Override
    public Integer[] findUserFavs(Map map) {
        return this.userFavoriteDao.findUserFavs(map);
    }

    @Override
    public Integer deleteUserFav(Map map) {
        return this.userFavoriteDao.deleteUserFav(map);
    }

    @Override
    public Integer[] findCourseStudents(Course course) {
        return this.userCourseDao.findCourseStudents(course);
    }

    @Override
    public Integer addClickNums(Integer id) {
        return this.courseDao.addClickNums(id);
    }

    @Override
    public Integer addClickNums_org(Integer id) {
        return this.courseDao.addClickNums_org(id);
    }

    @Override
    public Integer addClickNums_teacher(Integer id) {
        return this.courseDao.addClickNums_teacher(id);
    }

    @Override
    public Integer changeUserPassword(TempEmail tempEmail) {
        return this.userDao.changeUserPassword(tempEmail);
    }

    @Override
    public Integer addTeacher(Teacher teacher) {
        return this.teacherDao.addTeacher(teacher);
    }

    @Override
    public List<Teacher> findTeacherInBackGround(Teacher teacher) {
        return this.teacherDao.findTeacherInBackGround(teacher);
    }

    @Override
    public Integer removeTeacher(Integer id) {
        return this.teacherDao.removeTeacher(id);
    }

    @Override
    public Integer updateTeacher(Teacher teacher) {
        return this.teacherDao.updateTeacher(teacher);
    }

    @Override
    public Integer addBanner(Banner banner) {
        return this.adminDao.addBanner(banner);
    }

    @Override
    public List<Banner> findAllBanner() {
        return this.adminDao.findAllBanner();
    }

    @Override
    public Integer updateBanner(Banner banner) {
        return this.adminDao.updateBanner(banner);
    }

    @Override
    public List<Course> searchCourse(String name) {
        return this.courseDao.searchCourse(name);
    }
}
